//
//  ViewController.m
//  CoreDataLearn
//
//  Created by zhoutong on 17/4/12.
//  Copyright © 2017年 tong. All rights reserved.
//

#import "ViewController.h"
#import "ZTDataManager.h"
#import "Person+CoreDataClass.h"
#import <UIViewController+MJPopupViewController.h>
#import "AddDataVC.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *myTableView;

@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,strong) ZTDataManager *manager;

@end

@implementation ViewController

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
//        _dataArray = [NSMutableArray arrayWithCapacity:0];
        NSArray *arr = [[ZTDataManager shareDataManager] selectData:nil andSortType:nil];
        _dataArray = [NSMutableArray arrayWithArray:arr];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    _manager = [ZTDataManager shareDataManager];
    
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    
}
#pragma mark -- 增删改查
- (IBAction)addCell:(id)sender {
#if 0
    Person *obj = [NSEntityDescription insertNewObjectForEntityForName:ModelClass inManagedObjectContext:_manager.persistentContainer.viewContext];
    obj.name = @"佐助";
    obj.headImage = UIImagePNGRepresentation([UIImage imageNamed:@"01.png"]);
    obj.age = 15;
    obj.sex = YES;
    obj.time = [NSDate dateWithTimeIntervalSinceNow:0];
    
    [self.manager addData:@[obj]];
    [self.dataArray addObject:obj];
    [self.myTableView reloadData];

#else
    __weak typeof(self) weakSelf = self;
    [self presentPopupViewController:[[AddDataVC alloc] initWithDataBlock:^(NSMutableDictionary *dataDic) {
        
        [weakSelf.manager addData:^(Person *obj) {
            
            obj.name = [dataDic objectForKey:@"name"];
            obj.headImage = [dataDic objectForKey:@"headImage"];
            obj.age = [[dataDic objectForKey:@"age"] integerValue];
            obj.sex = [[dataDic objectForKey:@"sex"] boolValue];
            obj.time = [NSDate dateWithTimeIntervalSinceNow:0];
            [weakSelf.dataArray addObject:obj];
            
        }];
        
        [weakSelf.myTableView reloadData];
        
    }] animationType:MJPopupViewAnimationFade];
#endif
}
//编辑模式
static BOOL editMode = NO;
- (IBAction)deleteCell:(id)sender {
    
    editMode = !editMode;
    [self.myTableView setEditing:editMode animated:YES];
    /*
     全删除
    [_manager deleteData:[NSArray arrayWithArray:self.dataArray]];
    [self.dataArray removeAllObjects];
    [_myTableView reloadData];
    */
}
- (IBAction)updateCell:(id)sender {
    self.dataArray = nil;
    [self dataArray];
    [self.myTableView reloadData];
}
- (IBAction)selectCell:(id)sender {
    
}

#pragma mark -- 代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PersonCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    Person *obj = self.dataArray[indexPath.row];
    
    UIImageView *headImage = [cell viewWithTag:100];
    headImage.image = [UIImage imageWithData:obj.headImage];
    
    UILabel *name = [cell viewWithTag:101];
    name.text = obj.name;
    
    UILabel *age = [cell viewWithTag:102];
    age.text = [NSString stringWithFormat:@"年龄:%lld",obj.age];
    
    UILabel *sex = [cell viewWithTag:103];
    sex.text = obj.sex?@"男":@"女";
    
    UILabel *time = [cell viewWithTag:104];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm:ss"];
    NSString *currentDateStr = [dateFormatter stringFromDate:obj.time];
    time.text = currentDateStr;
    
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    Person *tmp = [self.dataArray objectAtIndex:indexPath.row];
    
    __weak typeof(self) weakSelf = self;
    
    AddDataVC *vc = [[AddDataVC alloc] initWithDataBlock:^(NSMutableDictionary *dataDic) {
        
        tmp.name = [dataDic objectForKey:@"name"];
        tmp.headImage = [dataDic objectForKey:@"headImage"];
        tmp.age = [[dataDic objectForKey:@"age"] integerValue];
        tmp.sex = [[dataDic objectForKey:@"sex"] boolValue];
        tmp.time = [NSDate dateWithTimeIntervalSinceNow:0];
        
        [weakSelf.manager updateData];
        
        [weakSelf.myTableView reloadData];
    }];
    
    [self presentPopupViewController:vc animationType:MJPopupViewAnimationFade];
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:5];
    [dic setObject:tmp.name forKey:@"name"];
    [dic setObject:[NSNumber numberWithInt:tmp.age] forKey:@"age"];
    [dic setObject:[NSNumber numberWithBool:tmp.sex] forKey:@"sex"];
    [dic setObject:tmp.headImage forKey:@"headImage"];
    
    [vc fillData:dic];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (UITableViewCellEditingStyleDelete == editingStyle) {
        
        //去除要删除的对象
        Person *obj = [self.dataArray objectAtIndex:indexPath.row];
        [self.manager deleteData:@[obj]];
        [self.dataArray removeObject:obj];
        
        [self.myTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
//        editMode = NO;
//        [self.myTableView setEditing:editMode];
    }
}

@end
